<?php
/**
 +------------------------------------------------------------------------------
 * KitMoney套件资产操作
 +------------------------------------------------------------------------------
 * @package  app
 * @author   alpha <QQ:411083658>
 * @date 2013-9-12
 * @time  下午13:38:07
 +------------------------------------------------------------------------------
 */
class KitMoney {
	protected $line = '';
	protected $isdebug = '';
	/**
	 * SMW01分销用户变动余额
	 * 
	 * @param int $userid
	 *        	用户ID
	 * @param string $subject
	 *        	财务科目 XV TV
	 * @param int $value
	 *        	充值数额 分为单位
	 * @param array $opt=array(
	 *        	'action_userid'=>'操作人',
	 *        	'cip'=>'IP',
	 *        	'bzvu'=>'订单号，授权码，操作原因',
	 *        	'changtype'=>'操作类型 ADMIN_ADD,KIT_ORDER_PAY_XV,KIT_ORDER_BAK,TENPAY_ADD,CVTOXV,TVTOXV,KIT_ORDER_RECE_TV,TVTORMB',
	 * 			'oper' => '-'
	 *        	)
	 * @return 当前余额 或 false
	 *         @date 2013-9-12
	 *         @time 下午3:40:06
	 */
	function recharge($userid = 0, $subject = '', $value = 0, $opt = array(), $isRet = false) {
		if($value>0){
			$value = $value * 100;
		}
		// 用户余额
		$this->line = 'recharge::' . __LINE__;
		if (empty ( $userid )) {
			self::_err ( '用户ID不能为空。' );
		}
		if (empty ( $subject )) {
			self::_err ( '参数错误。' );
		}
		if (empty ( $value ) || $value < 0) {
			self::_err ( '变动数额不能为0' );
		}
		if (empty ( $opt ['changtype'] )) {
			self::_err ( '变动类型必须填写' );
		}
		if (($opt ['changtype'] == 'ADMIN_ADD' || $opt ['changtype'] == 'ORDER_BAK' || $opt ['changtype'] == 'ADMIN_DECR') && empty ( $opt ['action_userid'] )) {
			self::_err ( '充值、退款操作时操作人ID不能为空。' );
		}
		$opt ['action_userid'] = isset ( $opt ['action_userid'] ) ? $opt ['action_userid'] : $userid;
		$assetc = array (
				'platform' => 'SMW01' 
		);
		$assetc ['changtype'] = $opt ['changtype'];
		$assetc ['action_userid'] = $opt ['action_userid'];
		$assetc ['cip'] = $opt ['cip'];
		$assetc ['bzvu'] = $opt ['bzvu'];
		$assetc ['userid'] = $userid;
		$assetc ['subject'] = $subject;
		$assetc ['ctime'] = time ();
		// 获取用户当前的余额
		$money = self::money_info ( $userid, 'SMW01', $subject );
				
		// 写入用户余额
		if (empty ( $money ) && $money !== false) {
			$money = 0;
		}
		
		if(empty($opt ['oper']) || $opt ['oper']=='+'){
			$money = $money + $value;
			$assetc ['value'] = $value;
		}
		if(!empty($opt ['oper']) && $opt ['oper']=='-'){
			if($money < $value){
				if($isRet==false){
					return false;
				}else{
					return -1;
				}
			}else{
				$money = $money - $value;
				$assetc ['value'] = '-'.$value;
			}
		}
		$where = array ();
		$where ['userid'] = $userid;
		$where ['subject'] = $subject;
		$where ['platform'] = 'SMW01';
		$data = $where;
		$usermoney = M ( 'KitAgentMoney' );
		$res = $usermoney->where ( $where )->find ();
		$usermoney->startTrans ();
		$data ['value'] = $money;
		$data ['mtime'] = time ();
		if ($res) {
			$data ['id'] = $res ['id'];
			$return = $usermoney->save ( $data );
		} else {
			$return = $usermoney->add ( $data );
		}
		if (empty ( $return )) {
			self::_err ( '账号操作错误。' );
		}
		$assetc ['nowvalue'] = $money;
		// 写入充值流水
		$assetc_dao = M ( "KitAgentAssets" );
		$assetc_dao->startTrans ();
		$assetc_id = $assetc_dao->add ( $assetc );
		if ($assetc_id) {
			$usermoney->commit ();
			$assetc_dao->commit ();
			return $money;
		} else {
			$usermoney->rollback ();
			$assetc_dao->rollback ();
			return false;
		}
	} // end recharge
	
	/**
	 * 获取用户余额
	 * 
	 * @param int $userid
	 *        	用户ID
	 * @param string $platform
	 *        	平台信息 SMW01
	 * @param string $subject
	 *        	财务科目 XV TV
	 *        	@date 2013-9-12
	 *        	@time 下午5:58:41
	 */
	function money_info($userid = 0, $platform = 'SMW01', $subject = 'all') {
		// 获取用户余额
		$this->line = 'money_info::' . __LINE__;
		$user = M ( "KitAgentMoney" );
		$where = array ();
		if ($platform != 'all') {
			$where ['platform'] = $platform;
		}
		$where ['userid'] = $userid;
		$info = $user->where ( $where )->select ();
		if (empty ( $info )) {
			return false;
		}
		$res = array ();
		foreach ( $info as $v ) {
			//dump($info);
			$res [$v ['platform']] [$v ['subject']] = $v ['value'];
		}
		if ($platform != 'all') {
			if ($subject != 'all') {
				return $res [$platform] [$subject];
			} else {
				return $res [$platform];
			}
		}
		return $res;
	} // end money_info
	
	/**
	 * 获取专营商提现申请冻结金额
	 * 
	 * @param int $userid
	 *        	用户ID
	 * @return 当前冻结余额 或 false
	 * */
	function money_frozen($userid = 0) {
		$user = M ( "KitAgentTvtormb" );
		$where = array ();
		$where ['userid'] = $userid;
		$where ['status'] = array('LT',2);;
		$info = $user->where ( $where )->select ();
		if (empty ( $info )) {
			return false;
		}
		$res = 0;
		foreach ($info as $key => $value) {
			$res += $value['moneyprice'];
		}
		if($res > 0){
			$res = $res / 100;
		}else{
			return false;
		}
		return $res;
	} // end money_frozen
	/**
	 * 用户财务流水
	 * 
	 * @param array $userids
	 *        	用户ID数组
	 *        	@date 2013-9-12
	 *        	@time 下午3:30:30
	 */
	function assets($userids = array(), $platform = 'SMW01', $limit = '0,20', $subject = '',$changtype='') {
		// 用户财务流水
		$dao = M ( "KitAgentAssets" );
		$where = array ();
		$where ['platform'] = $platform;
		$ids = array ();
		if (is_array ( $userids )) {
			foreach ( $userids as $v ) {
				$ids [] = $v ['id'];
			}
		} else {
			$ids [] = $userids;
		}
		if($userids != 'all'){
			$where ['userid'] = array (
					'IN',
					implode ( ',', $ids ) 
			);
		}
		
		if ($subject) {
			$where ['subject'] = array (
					'IN',
					$subject 
			);
		}
		if ($changtype) {
			$where ['changtype'] = array (
					'IN',
					$changtype 
			);
		}
		$res = array ();
		if ($limit == 'all') {
			return $dao->where ( $where )->count ();
		}
		$data = $dao->where ( $where )->order ( "id DESC" )->limit ( $limit )->select ();
		
		if($userids != 'all'){
			$temp=array();
			foreach ($data as $v) {
				$temp[$v['userid']][]=$v;
			}
			if (is_array ( $userids )) {
				$res = $temp;
			} else {
				$res = $temp [$userids];
			}
		}else{
			$res = $data;
		}
		return $res;
	} // end assets
	
	/**
	 * 用户转帐充值留言列表
	 * */
	function pay_leaveword($userids = array(), $limit = '0,20') {
		// 用户转帐充值留言表
		$dao = M ( "KitAgentBankPay" );
		$where = array ();
		$ids = array ();
		if (is_array ( $userids )) {
			foreach ( $userids as $v ) {
				$ids [] = $v ['id'];
			}
		} else {
			$ids [] = $userids;
		}
		if ( $userids ) {
			$where ['userid'] = array (
					'IN',
					implode ( ',', $ids ) 
			);
		}
		
		$res = array ();
		if ($limit == 'all') {
			return $dao->where ( $where )->count ();
		}
		$data = $dao->where ( $where )->order ( "createtime DESC" )->limit ( $limit )->select ();
		$temp=array();
		foreach ($data as $v) {
			$temp[$v['userid']][]=$v;
		}
		if (is_array ( $userids )) {
			$res = $temp;
		} else {
			$res = $temp [$userids];
		}
		return $res;
	} // end payleaveword 
	
	/**
	 * 用户提现申请留言列表
	 * */
	function tvtormblist($userids = array(), $limit = '0,20') {
		// 用户转帐充值留言表
		$dao = M ( "KitAgentTvtormb" );
		$where = array ();
		$ids = array ();
		if (is_array ( $userids )) {
			foreach ( $userids as $v ) {
				$ids [] = $v ['id'];
			}
		} else {
			$ids [] = $userids;
		}
		if ( $userids ) {
			$where ['userid'] = array (
					'IN',
					implode ( ',', $ids ) 
			);
		}
		
		$res = array ();
		if ($limit == 'all') {
			return $dao->where ( $where )->count ();
		}
		$data = $dao->where ( $where )->order ( "createtime DESC" )->limit ( $limit )->select ();
		$temp=array();
		foreach ($data as $v) {
			$temp[$v['userid']][]=$v;
		}
		if (is_array ( $userids )) {
			$res = $temp;
		} else {
			$res = $temp [$userids];
		}
		return $res;
	} // end tvtormblist 
	
	/**
	 * 输出错误信息
	 * @date 2013-5-6
	 * @time 下午5:19:24
	 */
	function _err($arr = array(), $str = "暂无相关信息") {
		// 请求接口类型异常
		if ($this->isdebug == 1) {
			echo '<br>输出的数据:<br><textarea rows="15" cols="120">';
		}
		if (is_array ( $arr ) && ! empty ( $arr )) {
			$res = array (
					"status" => 0,
					"msg" => $str,
					'data' => $arr,
					'line' => $this->line 
			);
		} else {
			$res = array (
					"status" => 0,
					"msg" => $str,
					'data' => $this->debug,
					'line' => $this->line 
			);
		}
		echo self::myjson ( $res );
		if ($this->isdebug == 1) {
			echo '</textarea>';
		}
		die ();
	} // end _err
	
	/**
	 * 请求成功返回
	 * @date 2011-11-15
	 * @time 上午08:10:19
	 */
	private function _suc($arr = array(), $str = "操作成功") {
		// 请求接口类型异常
		$res = array (
				"status" => 1,
				"msg" => $str,
				'data' => $this->debug 
		);
		if (! empty ( $arr )) {
			$res = array_merge ( $res, $arr );
		}
		if ($this->isdebug == 1) {
			echo '<br>输出的数据:<br><textarea rows="15" cols="120">';
		}
		echo self::myjson ( $res );
		if ($this->isdebug == 1) {
			echo '</textarea>';
		}
		die ();
	} // end _suc
	private function myjson($code) {
		$code = json_encode ( $this->urlencodeAry ( $code ) );
		return urldecode ( $code );
		//return $code;
	}
	private function urlencodeAry($data) {
		if (is_array ( $data )) {
			foreach ( $data as $key => $val ) {
				$data [$key] = $this->urlencodeAry ( $val );
			}
			return $data;
		} else {
			return urlencode ( $data );
		}
	}
}//end Money